package com.jianguo.web.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jianguo.common.core.domain.AjaxResult;
import com.jianguo.order.domain.PzOrder;
import com.jianguo.order.domain.PzOrderStepRecord;
import com.jianguo.order.domain.StepResult;
import com.jianguo.order.service.IPzOrderService;
import com.jianguo.order.service.IPzOrderStepRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.jianguo.common.core.domain.AjaxResult.success;

/**
 * @author xjunjie
 * @date 2024/04/12
 */
@RestController
@RequestMapping("/api/step")
public class StepApi {

    @Autowired
    private IPzOrderService pzOrderService;

    @Autowired
    private IPzOrderStepRecordService stepRecordService;


    /**
     * @param orderId
     * @return {@link AjaxResult}
     */
    @GetMapping(value = "/list")
    public AjaxResult getList(String orderId){
        List<PzOrderStepRecord> list = stepRecordService.list(new LambdaQueryWrapper<PzOrderStepRecord>()
                .eq(PzOrderStepRecord::getOrderId, orderId).orderByAsc(PzOrderStepRecord::getSort));
        List<StepResult> stepResults = new ArrayList<>();

        for (PzOrderStepRecord sp:list) {
            StepResult build = StepResult.builder().name(sp.getName())
                    .doTime(sp.getDoTime()).id(sp.getId()).build();
            stepResults.add(build);
        }
        return success(stepResults);
    }
    //更新流程
    @GetMapping(value = "/changeProcess")
    public AjaxResult changeProcess(String id){
        LambdaQueryWrapper<PzOrderStepRecord> queryWrapper = new LambdaQueryWrapper<PzOrderStepRecord>()
                .eq(PzOrderStepRecord::getId, id);
        PzOrderStepRecord one = stepRecordService.getOne(queryWrapper);
        Date time = new Date();
        one.setDoTime(time);
        one.setStatus(1L);
        stepRecordService.updateById(one);
        Long sort = one.getSort() - 1;
        PzOrderStepRecord one1 = stepRecordService.getOne(new LambdaQueryWrapper<PzOrderStepRecord>().
                eq(PzOrderStepRecord::getSort, sort).eq(PzOrderStepRecord::getOrderId, one.getOrderId()));

        one1.setStatus(0L);
        stepRecordService.updateById(one1);
        return success();
    }

    @GetMapping(value = "/judgeActive")
    public AjaxResult judgeActive(String id){
        LambdaQueryWrapper<PzOrderStepRecord> queryWrapper = new LambdaQueryWrapper<PzOrderStepRecord>()
                .eq(PzOrderStepRecord::getOrderId, id)
                .eq(PzOrderStepRecord::getStatus, 1);
        PzOrderStepRecord one = stepRecordService.getOne(queryWrapper);

        Long sort = one.getSort();

        return success(sort-1);
    }


}
